<template>
  <el-dialog
    :title="!dataForm.userId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户姓名" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户姓名"></el-input>
    </el-form-item>
    <el-form-item label="登录名" prop="loginName">
      <el-input v-model="dataForm.loginName" placeholder="登录名"></el-input>
    </el-form-item>
    <el-form-item label="登录密码" prop="loginPwd">
      <el-input v-model="dataForm.loginPwd" placeholder="登录密码"></el-input>
    </el-form-item>
    <el-form-item label="用户手机号" prop="userMobile">
      <el-input v-model="dataForm.userMobile" placeholder="用户手机号"></el-input>
    </el-form-item>
    <el-form-item label="用户余额" prop="balance">
      <el-input v-model="dataForm.balance" placeholder="用户余额"></el-input>
    </el-form-item>
    <el-form-item label="用户积分" prop="score">
      <el-input v-model="dataForm.score" placeholder="用户积分"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          userId: 0,
          userName: '',
          loginName: '',
          loginPwd: '',
          userMobile: '',
          balance: '',
          score: ''
        },
        dataRule: {
          userName: [
            { required: true, message: '用户姓名不能为空', trigger: 'blur' }
          ],
          loginName: [
            { required: true, message: '登录名不能为空', trigger: 'blur' }
          ],
          loginPwd: [
            { required: true, message: '登录密码不能为空', trigger: 'blur' }
          ],
          userMobile: [
            { required: true, message: '用户手机号不能为空', trigger: 'blur' }
          ],
          balance: [
            { required: true, message: '用户余额不能为空', trigger: 'blur' }
          ],
          score: [
            { required: true, message: '用户积分不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.userId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.userId) {
            this.$http({
              url: this.$http.adornUrl(`/seckill/user/info/${this.dataForm.userId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userName = data.user.userName
                this.dataForm.loginName = data.user.loginName
                this.dataForm.loginPwd = data.user.loginPwd
                this.dataForm.userMobile = data.user.userMobile
                this.dataForm.balance = data.user.balance
                this.dataForm.score = data.user.score
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/seckill/user/${!this.dataForm.userId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.userId || undefined,
                'userName': this.dataForm.userName,
                'loginName': this.dataForm.loginName,
                'loginPwd': this.dataForm.loginPwd,
                'userMobile': this.dataForm.userMobile,
                'balance': this.dataForm.balance,
                'score': this.dataForm.score
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
